import { Component, OnInit, OnDestroy } from '@angular/core';

import { DashCategorysService } from './dash.categorys.service';


import { Subscription } from 'rxjs';

import { Util } from '../../utils/util';
import { AppSettings } from '../../app.settings';
import { Scope } from "../../utils/emit";

import {CategoryName} from '../../pipes/categoryNamePipe';

@Component({
  selector: 'app-dashborad',
  templateUrl: './dashborad.page.html',
  styles: [`
  dash-card{
    display:block;
    margin-top: 10px;
    margin-bottom:10px;
  }
  .date{
    font-size:14px;
  }
  .pt40{
    padding-top:40px;
  }

  `],
  providers: []
})
export class DashboradPage implements OnInit {

  user: any;
  constructor(  private util: Util, private appSettings: AppSettings, private scope: Scope,private CategoryName:CategoryName) {

  }

  // category option
  option:any = {
    defaultText: true
  };

  // 用户关注的类目
  categorys: any = [];


  eventUpdateOff: any;

  // 类目 接收pie component cat change
  cat: any = {
    catName: "",
    wholeName: "",
    categoryId: ""
  };

  catInfo:any;

  // 添加
  addView: boolean = false;
  add() {
    this.addView = !this.addView;

    if (!this.addView) {
      this.getCategorys();
    }
  }

  addEvent:any;
  ngOnInit() {

    this.user = this.util.sessionStore().get('user');

    this.getCategorys();



    this.addEvent = this.scope.on('updateCategory',()=>{
      this.getCategorys();
    })
  }




  ngOnDestroy() {
    if (this.cardEvent) {
      this.cardEvent.unsubscribe();
    }
    if (this.catEvent) {
      this.catEvent.unsubscribe();

    }
    if(this.addEvent){
      this.addEvent.unsubscribe();
    }


    if (this.eventUpdateOff) {
      this.eventUpdateOff.unsubscribe();
    }

    if(this.catDobuleEvent){
      this.catDobuleEvent.unsubscribe();
    }
  }

  goLook(cat){

    this.cat.catName = this.CategoryName.transform(cat.value);
    this.cat.wholeName= cat.value;
    this.cat.categoryId= cat.target;
    // 在app里保存
    this.appSettings.setAppCat(this.cat);

    this.option.cat = this.cat;

    this.addView = !this.addView;

  }





  // out handel,pie的总开关
  handleUpdated(cat) {

    console.log('广播类目');

    console.log(cat);

    this.cat = cat;

    // 在app里保存
    this.appSettings.setAppCat(cat);

    this.getCatInfo(cat);
    this.getCatDoubelInfo(cat);

    // 广播类目
    this.scope.emit('dashborad-all-cur-cat', cat);
  }




  // 获取监控行业
  catEvent: Subscription;
  getCategorys() {

    var res = {"code":0,"msg":"成功","data":[{"id":964,"type":4,"target":"50019780","userId":5,"value":"数码/手机/家电--平板电脑/MID","createTime":1508895423000}]};

   


      if (res.data.length > 0) {
        for (let i in res.data) {
          let item:any = res.data[i];

          item.cat = {
            catName: item.value,
            wholeName: item.value,
            categoryId: item.target
          };
        }

        // list
        this.categorys = res.data;




      } else {

        this.categorys = [];

        this.addView = true;

      }

    
  }

  cardEvent:Subscription;
  getCatInfo(param){
    var res = {"code":0,"msg":"成功","data":{"value":[{"id":"50019780\u0001","date":"2017-12-03 00:00:00","categoryId":"50019780","categoryName":"数码/手机/家电--平板电脑/MID","categoryLevel":2,"brandName":"","sellAmount":5703,"tsellAmount":4026,"csellAmount":1574,"thksellAmount":0,"gsellAmount":103,"monthSellAmount":254425,"monthTSellAmount":210365,"monthCSellAmount":41924,"monthThkSellAmount":15,"monthGSellAmount":2121,"sellMoney":7868591.29,"tsellMoney":4316238.0,"csellMoney":3308763.23,"thksellMoney":0.0,"gsellMoney":243590.06,"monthSellMoney":4.1179106436E8,"monthTSellMoney":3.131679927E8,"monthCSellMoney":9.326285549E7,"monthThkSellMoney":5520.0,"monthGSellMoney":5354696.17,"productNum":3028,"tproductNum":1090,"cproductNum":1908,"thkproductNum":1,"gproductNum":29,"shopNum":708,"tshopNum":178,"cshopNum":517,"thkshopNum":1,"gshopNum":12}],"count":1}};

      if(res.data.value.length>0){
        this.catInfo = res.data.value[0];
      }else{
        this.catInfo = {};
      }

     
  }

  // 11.11预售
  catDoble:any = {
    sellAmount: 0,
    dingJin: 0,
    sellMoney: 0
  };
  catDobuleEvent: Subscription;
  getCatDoubelInfo(param){

    this.catDoble = {
      sellAmount: 0,
      dingJin: 0,
      sellMoney: 0
    };

    var res = {"code":0,"msg":"成功","data":{"sellAmount":98437,"dingJin":9843700.0,"sellMoney":2.32942369E8}};
      if(res.data){
        this.catDoble = res.data;
      }


    
  }


}
